package lunchy.mvc.views;

import java.util.HashMap;
import java.util.Map;

import lunchy.data.interfaces.ILunchyMenuGroup;
import lunchy.data.interfaces.ILunchyMenuItem;
import lunchy.main.CLunchyApplication;
import lunchy.mvc.controllers.CLunchyMenuController;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Spinner;

public class CLunchyMenuViewExample
{
	private Shell shellMenuExample;
	private CLunchyMenuController controller;
	private Table table;
	private Spinner editCount;
	
	private Map<TableItem, ILunchyMenuItem> itemsMap;
	
	public CLunchyMenuViewExample()
	{
		controller = new CLunchyMenuController();
		
		itemsMap = new HashMap<TableItem, ILunchyMenuItem>();
		
		shellMenuExample = new Shell(CLunchyApplication.getInstance().display, SWT.CLOSE | SWT.TITLE | SWT.MIN);
		shellMenuExample.setMinimumSize(new Point(200, 200));
		shellMenuExample.setText("LunchY");
		shellMenuExample.setSize(580, 454);
		
		initUI();
		
		shellMenuExample.open();
        
        while (!shellMenuExample.isDisposed())
        {
        	if (!CLunchyApplication.getInstance().display.readAndDispatch())
        		CLunchyApplication.getInstance().display.sleep();
        }
	}

	private void initUI()
	{
		table = new Table(shellMenuExample, SWT.BORDER | SWT.FULL_SELECTION);
		table.setBounds(10, 10, 560, 383);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(244);
		tableColumn.setText("Название");
		
		TableColumn tableColumn_1 = new TableColumn(table, SWT.NONE);
		tableColumn_1.setWidth(101);
		tableColumn_1.setText("Цена");
		
		TableColumn tableColumn_2 = new TableColumn(table, SWT.NONE);
		tableColumn_2.setWidth(110);
		tableColumn_2.setText("Количество");
		
		TableColumn tableColumn_3 = new TableColumn(table, SWT.NONE);
		tableColumn_3.setWidth(105);
		tableColumn_3.setText("Общая цена");
		
		Button button = new Button(shellMenuExample, SWT.NONE);
		button.setBounds(476, 399, 94, 28);
		button.setText("Заказать");
		
		editCount = new Spinner(shellMenuExample, SWT.BORDER);
		editCount.setBounds(10, 399, 94, 22);
		editCount.setEnabled(false);
		
		for (ILunchyMenuGroup currentGroup : controller.getMenuGroups())
		{
			////Group
		    TableItem tableGroup = new TableItem(table, SWT.NONE);
		    tableGroup.setText(currentGroup.getName());
		    tableGroup.setBackground(new Color(null, 100, 100, 100));
		    tableGroup.setForeground(new Color(null, 255, 255, 255));
			
		    for (ILunchyMenuItem currentItem : currentGroup.getItems())
		    {
		    	////Item
		    	TableItem tableItem = new TableItem(table, SWT.NONE);
		    	tableItem.setText(new String[] {currentItem.getName(), String.format("%d руб.", currentItem.getPrice()), "0", "0 руб."});
		    	itemsMap.put(tableItem,	currentItem);
		    }
		}
		
		table.addSelectionListener(
			new SelectionListener()
			{
				@Override
				public void widgetSelected(SelectionEvent e)
				{
					TableItem tableItem = (TableItem)e.item;
					ILunchyMenuItem item = itemsMap.get(tableItem);

					editCount.setSelection(Integer.parseInt(tableItem.getText(2)));
					
					if(item != null)
					{
						CLunchyApplication.getInstance().debug("Item selected: " + item.getName());
						editCount.setEnabled(true);
					}
					else
						editCount.setEnabled(false);
				}
				
				@Override
				public void widgetDefaultSelected(SelectionEvent e)
				{
				}
			}
		);
	}
}
